namespace Universal.Domain.Entities.App;

/// <summary>
/// 文章分类实体类  
/// </summary>
public class AppArticleCategory : EntityBase
{
    public string ArticleCategoryName { get; set; } = string.Empty;
    public string? Description { get; set; }
    public int Sort { get; set; } = 0;// 排序值

    //一个分类拥有多个子分类
    public Guid? ParentId { get; set; }
    //一个分类拥有一个父分类
    public AppArticleCategory? Parent { get; set; }

    //一个分类拥有多个子分类
    public ICollection<AppArticleCategory> Children { get; set; } = new List<AppArticleCategory>();

    //一个分类拥有多个文章
    public ICollection<AppArticle> Articles { get; set; } = new List<AppArticle>();

    /// <summary>
    /// 分配子分类
    /// </summary>
    /// <param name="childCategory">子分类</param>
    /// <returns>是否分配成功</returns>
    public bool AssignChild(AppArticleCategory childCategory)
    {
        //如果不包含则添加子分类
        if (!Children.Contains(childCategory))
        {
            Children.Add(childCategory);
            childCategory.ParentId = Id;
            childCategory.Parent = this;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 移除子分类
    /// </summary>
    /// <param name="childCategory">子分类</param>
    /// <returns>是否移除成功</returns>
    public bool RemoveChild(AppArticleCategory childCategory)
    {
        //如果包含则移除子分类
        if (Children.Contains(childCategory))
        {
            Children.Remove(childCategory);
            childCategory.ParentId = null;
            childCategory.Parent = null;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 分配文章到分类
    /// </summary>
    /// <param name="article">文章</param>
    /// <returns>是否分配成功</returns>
    public bool AssignArticle(AppArticle article)
    {
        //如果不包含则添加文章
        if (!Articles.Contains(article))
        {
            Articles.Add(article);
            article.CategoryId = Id;
            article.Category = this;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 从分类移除文章
    /// </summary>
    /// <param name="article">文章</param>
    /// <returns>是否移除成功</returns>
    public bool RemoveArticle(AppArticle article)
    {
        //如果包含则移除文章
        if (Articles.Contains(article))
        {
            Articles.Remove(article);
            article.CategoryId = null;
            article.Category = null;
            return true;
        }
        return false;
    }
}